package model.PokerTP;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import play.libs.Json;
import play.mvc.WebSocket;
import model.messages.*;

public class Human extends Player {
	private WebSocket.In<JsonNode> inSocket; 
	private WebSocket.Out<JsonNode> outSocket;
	
	public String name;
	public Human(){
		super();
	}
	public Human(Deck d) {
		super(d);
	}
	
	void imie(String s){
		name=s;
	}
	public String toString (){
		return name;
	}
	
	public Human(String name, Deck deck, WebSocket.In<JsonNode> inSock, WebSocket.Out<JsonNode> outSock) {
		super(deck);
		if(name == null || inSock == null || outSock == null) throw new NullPointerException();
		this.name = name;
		inSocket = inSock;
		outSocket = outSock;
	}
	
	@Override
	public void wykonajSwojRuch() {
		int c=0;
		int k=0;
		super.wykonajSwojRuch();
		wyswietl();
		System.out.println(zamianaKart());
		
		//pobiera ilosc kart do wymiany
		String s = "";
		c = -1;
		while(c < 0 || c > 4) // mozna wymienic max. 4 karty
		{
			System.out.println("Ile kart wymieniasz (0-4)");
			try{
				BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
			    s = bufferRead.readLine();
			    c=Integer.parseInt(s);
			}catch(NumberFormatException ex){
				System.out.println(s + " - nie jest poprawna liczba.");
			}
			catch(Exception ex2)
			{
				System.out.println("Wystapil blad: " + ex2.getMessage());
			}
		}
		//System.out.println(" wymieniasz "+c);
		//wymienia karty 
		
		for(int i=0;i<c;i++){
			try{
				BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
				s = bufferRead.readLine();
				k=Integer.parseInt(s);
				zmien(k); // ALBO: zmien(k, talia);
			}catch(NumberFormatException ex){
				System.out.println(s + " - nie jest poprawna liczba.");
				continue;
			}
			catch(Blad bl)
			{
				System.out.println("BLAD: " + bl.getMessage());
				continue;
			}
			catch(Exception ex2)
			{
				System.out.println("Wystapil blad: " + ex2.getMessage());
				continue;
			}
			
		}
		
		wyswietl();
		System.out.println(zamianaKart());
	}
	public String licytuj(){
	    String s ="";
	    try{
            BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
            s = bufferRead.readLine();
        }catch(NumberFormatException ex){
            System.out.println(s + " - nie jest poprawna liczba.");
        }
        catch(Exception ex2)
        {
            System.out.println("Wystapil blad: " + ex2.getMessage());
        }
	    return s;
	}
	    
	public int ile(){
	    String s ="";
	    int c=0;
        try{
            BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
            s = bufferRead.readLine();
            c=Integer.parseInt(s);
        }catch(NumberFormatException ex){
            System.out.println(s + " - nie jest poprawna liczba.");
        }
        catch(Exception ex2)
        {
            System.out.println("Wystapil blad: " + ex2.getMessage());
        }
        return c;
	}
	@Override
	public void onReceive(Object message) throws Exception {
		// TODO Automatycznie generowany szkielet metody
		if(message instanceof NeedInputData) {
			needInputData((NeedInputData)message);
		}
		else if(message instanceof TablePopupMessage) {
			tablePopupMessage((TablePopupMessage)message);
		}
		else if(message instanceof TableMessage) {
			tableMessage((TableMessage)message);
		}
		else
			unhandled(message);
		
	}
	
	private synchronized void tableMessage(TableMessage message) {
		// TODO Automatycznie generowany szkielet metody
		ObjectNode node = Json.newObject();
		node.put("action", "popup");
		node.put("actionMessage", message.message);
		outSocket.write(node);
		
	}
	private synchronized void tablePopupMessage(TablePopupMessage message) {
		// TODO Automatycznie generowany szkielet metody
		ObjectNode node = Json.newObject();
		node.put("action", "msg");
		node.put("actionMessage", message.message);
	}
	private synchronized void needInputData(NeedInputData msg) {
		String argN = msg.argumentName;
		if(argN.compareTo("ile") == 0) {
			ObjectNode node = Json.newObject();
			node.put("action", "inputDialog");
			node.put("actionMessage", msg.message);
			outSocket.write(node);
		}
		else;
	}
}
	
	

